package controller;

import dao.PlanoJpaController;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import dao.exceptions.NonexistentEntityException;
import model.Plano;
import model.Turma;
import pagecontroller.JsfUtil;

/**
 *
 * @author Edilson Fernandes da Costa
 * @version 1.0
 */
public class PlanoController extends Controller {

    public PlanoController() {
        if (JsfUtil.getObjectFromSession("PlanoJpaController") == null) {
            jpaController = new PlanoJpaController();
        } else {
            jpaController = (PlanoJpaController) JsfUtil.getObjectFromSession("PlanoJpaController");
        }
    }
    private Plano plano = null;
    private Plano planoAux = null;
    private PlanoJpaController jpaController = null;
    private List<Plano> planos = null;
    private Turma turma;

    public String persiste() {
        if (plano.getId() == null) {
            return create();
        } else {
            return update();
        }
    }

    public String update() {
        try {
            jpaController.edit(this.plano);
            planoAux = plano;
            this.plano = null;
            JsfUtil.addSuccessMessage("Plano Atualizado com Sucesso");
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(PlanoController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(PlanoController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "refreshPage";
    }

    public String create() {
        try {
            jpaController.create(plano);
            planoAux = plano;
            plano = null;
            JsfUtil.addSuccessMessage("Plano cadastrado com sucesso.");
            getPageManager().setPagePlanoRelat();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "Ocorreu um erro na percistencia dos dados.");
            return null;
        }
        return "refreshPage";
    }

    public String edit() {
        Plano planoTemp = (Plano) JsfUtil.getObjectFromRequestParameter("plano");
        this.plano = planoTemp;
        return "refreshPage";
    }

    public String destroy() {
        Plano planoTemp = (Plano) JsfUtil.getObjectFromRequestParameter("plano");
        try {
            jpaController.destroy(planoTemp.getId());
            JsfUtil.addSuccessMessage("Plano removido com sucesso.");
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(PlanoController.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "refreshPage";
    }

    public String destroyButton() {
        Plano planoTemp = planoAux;
        try {
            jpaController.destroy(planoTemp.getId());
            JsfUtil.addSuccessMessage("Plano removida com sucesso.");
            planoAux = null;

        } catch (NonexistentEntityException ex) {
            Logger.getLogger(AlunoController.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "refreshPage";
    }

    public String load() {
        planoAux = (Plano) JsfUtil.getObjectFromRequestParameter("planoTemp");
        getPageManager().setPagePlanoRelat();
        return "refreshPage";
    }

    public String editButton() {
        this.plano = planoAux;
        getPageManager().setPagePlanoCad();

        return "refreshPage";
    }

    public Plano getPlano() {
        if (plano == null) {
            plano = new Plano();
            plano.setTurma(getTurma());
        }
        return plano;
    }

    public List<Plano> getPlanos() {
        this.planos = jpaController.findPlanoEntities();
        return this.planos;
    }

    public PlanoJpaController getJpaController() {
        return jpaController;
    }

    public Plano getPlanoAux() {
        return planoAux;
    }

    public Turma getTurma() {

        if (turma == null) {
            TurmaController turmaController = (TurmaController) JsfUtil.getObjectFromSession("TurmaController");
            if (turmaController == null) {
                turmaController = new TurmaController();
            }
            turma = turmaController.getTurmaAux();
        }
        return turma;
    }

    public void setTurma(Turma turma) {
        this.turma = turma;
    }

    public Plano getPlanoDaTurma(Turma turmaAux) {

        return jpaController.findPlano(Integer.SIZE);
    
    }
}
